﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Net.Mail;

namespace MainApp
{
    public partial class FrmSendingMail : DevExpress.XtraEditors.XtraForm
    {
        public FrmSendingMail()
        {
            InitializeComponent();
        }

        public void sendReport(string fileName, string subject, string body) 
        {
            try
            {
                txtInfo.Text = string.Format("Đang gởi báo cáo đến {0}. Xin vui lòng đợi ...", Properties.Settings.Default.emailAddress);
                Show();
                Update();
                MailMessage mes = new MailMessage(Properties.Settings.Default.usingAddress, Properties.Settings.Default.emailAddress, subject, body);

                Attachment att = new Attachment(fileName);
                mes.Attachments.Add(att);

                SmtpClient client = new SmtpClient("smtp.gmail.com", 25);
                client.EnableSsl = true;
                client.Credentials = new System.Net.NetworkCredential(Properties.Settings.Default.usingAddress.Replace("@gmail.com", ""), "TNTDouble");
                client.Send(mes);
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show("Không thể gởi được email, vui lòng kiểm tra kết nối Internet\nChi tiết lỗi:" + ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                Close();
            }
        }
    }    
}